import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:smart_devices/model/repair_order.dart';

class OrderLogic extends GetxController {

  late RefreshController refreshController;

  RxList<RepairOrderModel> pageList = RxList([]);

  @override
  void onInit() {
    super.onInit();
    for (var i = 0; i < 10; i++) {
      pageList.add(RepairOrderModel(
          title: '电梯设备维修02',
          orderNo: 'BX-2023041648',
          name: '电梯设备',
          code: 'SB0792789',
          location: '生产车间二层',
          degree: getDegreeByIndex(i),
          addTime: '2023.11.14 08:32:31',
          status: i % 2 == 0 ? RepairOrderEnum.fast : RepairOrderEnum.repair));
    }
    refreshController = RefreshController(initialRefresh: false);
  }

  RepairOrderDegreeEnum getDegreeByIndex(int index) {
    var mode = index % RepairOrderDegreeEnum.values.length;
    switch (mode) {
      case 0:
        return RepairOrderDegreeEnum.slow;
      case 1:
        return RepairOrderDegreeEnum.mid;
      default:
        return RepairOrderDegreeEnum.fast;
    }
  }

  @override
  void onClose() {
    super.onClose();
    refreshController.dispose();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 15) {
      return refreshController.loadNoData();
    }

    /// 添加
    refreshController.loadComplete();
    pageList.add(RepairOrderModel(
        title: '电梯设备维修02',
        orderNo: 'BX-2023041648',
        name: '电梯设备',
        code: 'SB0792789',
        location: '生产车间二层',
        degree: RepairOrderDegreeEnum.mid,
        addTime: '2023.11.14 08:32:31',
        status: RepairOrderEnum.fast));
  }
}
